package engine.newUi
{
	import com.greensock.TweenLite;
	
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.flag.GameDefine;
	import engine.logic.ShowDamageNum;
	import engine.scene.player.BasePlayerModel;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import newx.com.config.StaticIni;
	
	/**
	 * 雪人逻辑 
	 * @author gf
	 * 
	 */	
	
	public class SnowBossBloodUI extends BaseUI
	{
		private static var self			:SnowBossBloodUI;
		
		/**boss血条界面*/		
		public var RoleMc					:MovieClip;
		
		private var model					:ModelLocator;
		private var bind					:BindManage;
		public var npcModel				:BasePlayerModel;
		
		private var hpIndex				:int		=	-1;
		
		private var hpWidth				:Number		=	0;
		
		public var isDead					:Boolean	=	false;
		
		public var damageEffect			:MovieClip;
		
		private var isFirst				:Boolean	=	true;
		
		private var lastHP					:int;
		
		/**间隔时间*/		
		private var damageTime				:int		=	1000;
		
		private var lastTime				:int;
		
		public function SnowBossBloodUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		
		public static function getInstance():SnowBossBloodUI
		{
			if(self == null)
			{
				self 		= 	new SnowBossBloodUI();
			}
			return self;
		}
		
		
		override public function Init():void
		{
			model			=	ModelLocator.getInstance();
			bind			=	BindManage.getInstance();
			hpWidth			=	RoleMc["hpLine"].width;
			damageEffect	=	new MovieClip();
			addChild(damageEffect);
		}
		
		override protected function BeforeShow():void
		{
			if(npcModel	==	null)
			{
				RoleMc["lvText"].htmlText 		= 	"?";
				RoleMc["nameText"].htmlText 	= 	"???";
				
				RoleMc["hpText"].htmlText		=	"???/???" ;
				RoleMc["hpLine"].width			=	0;
			}
		}
		
		override protected function AfterHide():void
		{
			if(GameData.SceneType == GameDefine.SceneType_SnowBallFightScene) return;
			if(hpIndex != -1)
			{
				bind.UnbindProp(npcModel.playerObj,"HP",hpIndex);
			}
			npcModel	=	null;
			
			reset();
			isFirst		=	true;
		}
		
		private function reset():void
		{
			while (damageEffect.numChildren > 0)
			{
				damageEffect.removeChildAt(0);
			}
			while(RoleMc["face"].numChildren >0)
			{
				RoleMc["face"].removeChildAt(0);
			}
		}
		
		public function update():void
		{
			RoleMc["lvText"].htmlText 		= 	npcModel.playerObj.GetPropertiesEx("Level");
			RoleMc["nameText"].htmlText 	= 	StaticIni.getSwfLangStr(npcModel.playerObj.GetPropertiesEx("NameID"));
			
			hpIndex							=	bind.BindProp(npcModel.playerObj,"HP",this,"changeBlood");
		}
		
		
		public function set changeBlood(val:String):void
		{
			var currentTime		:int		=	getTimer();
			var internalTime	:int		=	currentTime	-	lastTime;
			var currentHP		:int		=	int(val);
			var totalHP			:int		=	int(npcModel.playerObj.GetPropertiesEx("MaxHP"));
			RoleMc["hpText"].htmlText		=	currentHP + "/" + totalHP ;
			var hpLineWidth			:int	=	int(hpWidth * currentHP / totalHP);
			TweenLite.to(RoleMc["hpLine"],1,{width:hpLineWidth});
			
			var lostHP			:int		=	lastHP	-	currentHP;
			if(!isFirst && lostHP>0)
			{
				if(internalTime	>=	damageTime)
				{
					ShowDamageNum.ShowDamageEffect( lostHP , new Point( RoleMc.x+RoleMc.width-15, RoleMc.y+RoleMc.height-40 ) ,damageEffect,0,50);
				}
			}
			isFirst							=	false;
			
			lastHP							=	currentHP;
			lastTime						=	currentTime;
		}
	}
}
